package com.ftg.elvis.controller;

import com.ftg.elvis.pojo.Menu;
import com.ftg.elvis.pojo.Role;
import com.ftg.elvis.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/role")
public class AdminRoleController {

	@Autowired
	AdminRoleService adminRoleService;

	/**
	 * 查看所有角色
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("/showAll")
	public Map<String, Object> showAll(int page, int limit){
		List<Role> list =  adminRoleService.showAll(page, limit);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("count", adminRoleService.getCount());
		map.put("data", list);
		return map;
	}

	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@PostMapping("/add")
	public int add(Role role){
		return adminRoleService.add(role);
	}

	/**
	 * 删除角色
	 * @param rid
	 * @return
	 */
	@DeleteMapping("/del/{rid}")
	public int delete(@PathVariable String rid){
		return adminRoleService.delete(rid);
	}

	/**
	 * 修改角色信息
	 * @param role
	 * @return
	 */
	@PostMapping("/update")
	public int update(Role role){
		return adminRoleService.update(role);
	}

	/**
	 * 修改某个角色的权限
	 * @param role_id
	 * @param menu_ids
	 * @return
	 */
	@GetMapping("/updateMenus/{role_id}/{menu_ids}")
	public int updateMenus(@PathVariable String role_id, @PathVariable String[] menu_ids){
		return adminRoleService.updateMenus(role_id, menu_ids);
	}

	/**
	 * 查看某个角色已经拥有的权限
	 * @param role_id
	 * @return
	 */
	@GetMapping("/showMyMenus")
	public Map<String, Object> showMyMenus(String role_id){
		Map<String, Object> map = new HashMap<>();
		List<Menu> myMenus = adminRoleService.showMyMenus(role_id);
		List<Menu> allMenus = adminRoleService.showAllMenus();
		map.put("code", 0);
		//所有权限
		map.put("data1", allMenus);
		//我的权限
		map.put("data2", myMenus);
		return map;
	}

	/**
	 * 查看所有权限
	 * @return
	 */
	@GetMapping("/showAllMenus")
	public Map<String, Object> showAllMenus(){
		Map<String, Object> map = new HashMap<>();
		List<Menu> allMenus = adminRoleService.showAllMenus();
		map.put("code", 0);
		map.put("data", allMenus);

		return map;
	}

}
